package com.lxp.uCenterService.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lxp.servicebase.exception.MyException;
import com.lxp.uCenterService.domain.AdminUser;
import com.lxp.uCenterService.mapper.AdminUserMapper;
import com.lxp.uCenterService.service.AdminUserService;
import com.lxp.utils.JwtUtils;
import com.lxp.utils.MD5;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * <p>
 * 管理员表 服务实现类
 * </p>
 *
 * @author lxp
 * @since 2022-03-09
 */
@Service
public class AdminUserServiceImpl extends ServiceImpl<AdminUserMapper, AdminUser> implements AdminUserService {
    //登录
    @Override
    public String login(AdminUser user) {

        String username= user.getUsername();
        String password=user.getPassword();
        //判断输入是否为空
        if (StringUtils.isEmpty(username) || StringUtils.isEmpty(password)){
            throw new MyException(20001,"登录出错");
        }
        //根据username获取到用户
        QueryWrapper<AdminUser> wrapper =new QueryWrapper<>();
        wrapper.eq("username",username);
        AdminUser dataUser = baseMapper.selectOne(wrapper);
        if (dataUser == null){
            throw new MyException(20001,"用户名或密码错误!!!");
        }

        //判断密码和数据库对象的密码是否相同
        if (!MD5.encrypt(password).equals(dataUser.getPassword())){
            //密码不对
            throw new MyException(20001,"用户名或密码错误!!!");
        }
        //
        return  JwtUtils.getJwtToken(user.getId(), user.getUsername());
    }
}
